package CommonsdbutilsTest;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class poketmoneyDao {
    private QueryRunner queryRunner = new QueryRunner();




    /*
     * 1.查询所有的数据
     * */
    public List<Student> selectAll() {

        try {
            List<Student> emps = queryRunner.query(JDBCUtil.getconnection(),
                    "select * from  student ;",
                    new BeanListHandler<Student>(Student.class));
            System.out.println("emps = " + emps);
            ArrayList<Object> all = new ArrayList<>();
            all.add(emps);
            System.out.println("all = " + all);
            return emps;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    /*
     * 2.实现删除的业务逻辑接口
     * */
    public int delete(int id) {
        try {
            int result = queryRunner.update(JDBCUtil.getconnection(),
                    "delete from student where sid=?;", id);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /*
     * 3.更新的业务逻辑
     * */

    public int update(Student student) {
        try {
//            int result =queryRunner.update(DbUtils.getConnection(),
//                    "update empset name=?,salary=?,age=? where id=?",
//                    emp.getName(),emp.getSalary(), emp.getAge(), emp.getId());
            int result = queryRunner.update(JDBCUtil.getconnection(),
                    "update student set sname=?,ssex=?,sage=? ,sdepartment=? where sid=?;",
                    student.getSname(),student.getSsex(),student.getSage(),student.getSdepartment(),student.getSid());

            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /*
     * 选择一条数据
     * */

    public Student select(int id) {
        System.out.println("数据操作层：id = " + id);
        try {
//            Emp emp =queryRunner.query(DbUtils.getConnection(),
//                    "select * fromemp where id=?;",
//                    newBeanHandler<Emp>(Emp.class),id);

            Student student = queryRunner.query(JDBCUtil.getconnection(),
                    "select* from student where sid=?;",
                    new BeanHandler<Student>(Student.class), id);

            return student;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /*
     * 插入一条数据
     * */

    public int insert(Student student) {
        try {
//            int result =queryRunner.update(DbUtils.getConnection(),
//                    "update empset name=?,salary=?,age=? where id=?",
//                    emp.getName(),emp.getSalary(), emp.getAge(), emp.getId());
            int result = queryRunner.update(JDBCUtil.getconnection(),
                    "insert into student(sname,ssex,sage,sdepartment) values(?,?,?,?);",
                    student.getSname(),student.getSsex(),student.getSage(),student.getSdepartment());
            System.out.println("数据入库成功啦--result =" + result);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
    public void logon() throws Exception {
        money_javabean moneyJavabean = new money_javabean();
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入您的用户名");
        String name = scanner.next();
        System.out.println("请输入您的密码");
        String password = scanner.next();

        String sql = "select * from money where name=? && password=?";
        //4.创建数据操作对象
        Connection getconnection = JDBCUtil.getconnection();
        PreparedStatement statement = getconnection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setString(2, password);
        //5.执行sql，获取结果集
        ResultSet resultSet = statement.executeQuery();

        if (resultSet.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败,请重新登录");
            poketmoneyDao poketmoneyDao = new poketmoneyDao();
            poketmoneyDao.logon();


        }
    }




}
